﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace ME860.DotNetty.Common
{
    public class ApplicationConfig
    {
        #region 配置初始化

        private static IConfiguration _configuration;

        public static IConfiguration Configuration
        {
            get { return _configuration ?? ConfigurationBuilder(); }
        }
        private const string DefaultConfigFileName = "appsetting";
        private const string DefaultConfigFileSuffix = "json";

        /// <summary>
        /// 配置生成
        /// </summary>
        /// <param name="targetConfig"></param>
        private static IConfiguration ConfigurationBuilder(string targetConfig = null)
        {
            var appConfigFile = string.IsNullOrEmpty(targetConfig)
                ? $"{DefaultConfigFileName}.{DefaultConfigFileSuffix}"
                : $"{DefaultConfigFileName}.{targetConfig}.{DefaultConfigFileSuffix}";

            var builder = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile(appConfigFile);

            _configuration = builder.Build();
            return _configuration;
        }
        #endregion

        #region 配置

        #region 服务器配置
        private static ServerConfig _serverConfig;
        /// <summary>
        /// 服务器配置
        /// </summary>
        public static ServerConfig ServerConfig
        {
            get
            {
                if (_serverConfig == null)
                {
                    _serverConfig = new ServerConfig()
                    {
                        Host = Configuration["ServerConfig:Host"],
                        Port = Convert.ToInt32(Configuration["ServerConfig:Port"])
                    };
                }
                return _serverConfig;
            }
        }
        #endregion

        #endregion


    }
}
